package com.dkd.manage.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dkd.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.Order;
import com.dkd.manage.service.IOrderService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.dkd.common.core.domain.R;


/**
 * 订单Controller
 *
 * @author yuwei
 * @date 2024-09-15
 */
@RestController
@RequestMapping("/manage/order")
@Api(tags = "订单接口")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单列表
     */
    @ApiOperation(value = "获取订单列表", notes = "返回订单列表")
    @PreAuthorize("@ss.hasPermi('manage:order:list')")
//    @Anonymous
//    @PreAuthorize("@ss.hasRole('accountant')")
//    @PreAuthorize("@ss.lacksPermi('manage:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @ApiOperation(value = "导出订单列表", notes = "导出订单至Excel文件")
    @PreAuthorize("@ss.hasPermi('manage:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @ApiOperation(value = "获取订单详细信息", notes = "根据ID获取订单详细信息")
    @ApiParam(name = "id", value = "订单ID", required = true)
    @PreAuthorize("@ss.hasPermi('manage:order:query')")
    @GetMapping(value = "/{id}")
    public R<Order> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(orderService.selectOrderById(id));
    }

    /**
     * 新增订单
     */
    @ApiOperation(value = "新增订单", notes = "创建新的订单")
    @PreAuthorize("@ss.hasPermi('manage:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody Order order)
    {
        if(orderService.insertOrder(order)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    /**
     * 修改订单
     */
    @ApiOperation(value = "修改订单", notes = "根据ID修改订单")
    @PreAuthorize("@ss.hasPermi('manage:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody Order order)
    {
        if(orderService.updateOrder(order)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    /**
     * 删除订单
     */
    @ApiOperation(value = "删除改订单", notes = "根据ID删除订单")
    @PreAuthorize("@ss.hasPermi('manage:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        if(orderService.deleteOrderByIds(ids)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }
}


